﻿using System;
using KeySerialization.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace KeySerialization
{
    public class Program
    {
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            string serializedkey;

            using (var session = domain.OpenSession()) {
                using (var t = session.OpenTransaction()) {

                    var person = new Person(session) {
                        Name = "John Smith"
                    };

                    // Serializing key to string
                    serializedkey = person.Key.Format();
                    Console.WriteLine("Serialized key: {0}", serializedkey);
                    t.Complete();
                }
            }

            using (var session = domain.OpenSession()) {
                using (var t = session.OpenTransaction()) {

                    // Deserializing key from string
                    var key = Key.Parse(domain, serializedkey);
                    var person = session.Query.Single<Person>(key);

                    Console.WriteLine("Person: {0}", person.Name);
                    t.Complete();
                }
            }
        }
    }
}
